package com.meilai.project.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 张驰
 * @date 2021/6/29 15:44
 */
public class StringChangeUtil {
    private static final Pattern linePattern = Pattern.compile("_(\\w)");
    private static final Pattern humpPattern = Pattern.compile("[A-Z]");
    private static final Pattern intPattern = Pattern.compile("^[-+]?[\\d]*$");
    private static final Pattern doublePattern = Pattern.compile("^[-+]?\\d*[.]\\d+$");

    /** 判断整数*/
    public static boolean isInteger(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        return intPattern.matcher(str).matches();
    }

    /** 判断浮点数*/
    public static boolean isDouble(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        return doublePattern.matcher(str).matches();
    }

    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 驼峰转下划线,效率比上面高 */
    public static String humpToLine2(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
